//
// Created on 2024/11/17.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include <fstream>

#include "file_system.h"

bool ReadFile(std::string const &path, std::vector<std::string> &lines)
{
    std::ifstream ifs(path);
    if (!ifs.is_open()) {
        return false;
    }
    
    std::string buf;
    while (std::getline(ifs, buf)) {
        lines.push_back(std::move(buf));
    }
    return true;
}

bool WriteFile(const std::string &path, const std::vector<std::string> &lines)
{
    std::ofstream ofs(path, std::ios::out);
    if (!ofs.is_open()) {
        return false;
    }
    
    for (auto const &line: lines) {
        ofs.write(line.c_str(), line.size());
        ofs.write("\n", 1);
    }
    return true;
}